﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
using System.Web.Script.Serialization;
using System.Collections;

namespace ZX.WebApp.cspModel
{
    public class CodeCompleteString
    {

        public CodeCompleteString() { }
        public string CodeSmithCompleteString(string FormName, string tbname, string formTalbeJSON, string colnum, string FormType)
        {
            //主键ID#0;附件名称#1;附件体#2;附件保存路径#3;附件类型#4;
            string strForm = "";
            if (!string.IsNullOrEmpty(formTalbeJSON))
            {
                System.Data.DataTable dt = Json2Dtb(formTalbeJSON);
                strForm = CodeSmithAutoCode.CodeSmithComplete(dt, colnum, FormName, tbname, FormType);

            }
            return strForm;
        }
        /// <summary>    
        /// Json转DataTable    
        /// </summary>    
        /// <param name="json"></param>   
        /// /// <returns></returns>    
        private System.Data.DataTable Json2Dtb(string json)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ArrayList dic = jss.Deserialize<ArrayList>(json);
            System.Data.DataTable dtb = new System.Data.DataTable();
            if (dic.Count > 0)
            {
                foreach (Dictionary<string, object> drow in dic)
                {
                    if (dtb.Columns.Count == 0)
                    {
                        foreach (string key in drow.Keys)
                        {
                            dtb.Columns.Add(key, drow[key].GetType());
                        }
                    }
                    System.Data.DataRow row = dtb.NewRow();
                    foreach (string key in drow.Keys)
                    {
                        row[key] = drow[key];
                    }
                    dtb.Rows.Add(row);
                }
            }
            return dtb;
        }
    }
}